﻿package fwk.form{
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import fwk.interfaces.IRadioCheckQuestion;
	
	public class RadioCheckQuestion extends MovieClip implements IRadioCheckQuestion 
	{
		private var _mcClipContainer:MovieClip;
		private var _containerReponses:MovieClip;
		private var _id:String;
		private var _label:String;
		
		function RadioCheckQuestion(mc:MovieClip = null){
			trace("--> instance de RadioCheckQuestion");
			if(mc != null) this._mcClipContainer = mc;
			else this._mcClipContainer = this;
			
			_containerReponses = _mcClipContainer;
			
		}
		//--------------------------------------------------------
		public function set id(val:String)
		{
			_id = val;
		}
		public function get id():String
		{
			return _id;
		}
		//--------------------------------------------------------
		public function get label():String
		{			
			return _label;
		}
		//--------------------------------------------------------
		public function set label(lbl:String)
		{			
			_label = lbl;
		}
		public function get containerReponses():MovieClip
		{
			return _containerReponses;
		}
		public function set containerReponses(clip:MovieClip)
		{
			_containerReponses = clip;
		}
		//--------------------------------------------------------
		public function get reponsesTab():Array
		{
			var tabRep:Array = new Array();

			for (var i=0; i < _containerReponses.numChildren;i++ )
			{
				var clip:MovieClip = (_containerReponses.getChildAt(i) as MovieClip);
				
				if (clip.selected == true)
				{
					var objRep:Object 	= new Object();
					objRep.label 		= clip.label;
					objRep.id 			= clip.id;
					objRep.value 		= clip.value;
					tabRep.push(objRep);
				}

			}
			
			return tabRep;
		}
		//--------------------------------------------------------
		public function addReponse(itemRep:MovieClip)
		{

			itemRep.name = "rep" + itemRep.id;
			itemRep.buttonMode = true;
			itemRep.selected = false;
			itemRep.addEventListener(MouseEvent.MOUSE_DOWN, onClickReponse);
			_containerReponses.addChild(itemRep);
		}
		//--------------------------------------------------------
		public function onClickReponse(evt:MouseEvent)
		{
			trace(evt.currentTarget.selected);
			trace(evt.currentTarget.type);
			
			var type:String = evt.currentTarget.type;
			
			if (evt.currentTarget.selected == true)
			{
				switch (type)
				{
					case "check":
						trace("out");
						evt.currentTarget.selected = false;
						evt.currentTarget.gotoAndPlay("out");
						break;
					default: 
						break;
				}
				
			}else
			{	
				switch (type) 
				{
					case "radio":
						var i:int = 0;
						var clip:MovieClip;
						
						for (i=0; i < _containerReponses.numChildren;i++ )
						{
							clip = (_containerReponses.getChildAt(i) as MovieClip);
							if (clip.selected == true && clip.type != "check" )
							{
								clip.selected = false;
								clip.gotoAndPlay("out");
							}

						}
						break;
						
					case "check":
						for (i=0; i < _containerReponses.numChildren;i++ )
						{
							clip = _containerReponses.getChildAt(i) as MovieClip;
							if (clip.selected == true && clip.type == "init" )
							{
								clip.selected = false;
								clip.gotoAndPlay("out");
							}

						}
						break;
					
					case "init":
						for (i=0; i < _containerReponses.numChildren;i++ )
						{
							if ((_containerReponses.getChildAt(i) as Object).selected == true)
							{
								(_containerReponses.getChildAt(i) as MovieClip).selected = false;
								(_containerReponses.getChildAt(i) as MovieClip).gotoAndPlay("out");
							}

						}
						break;
					default: 
						break;
				}

				evt.currentTarget.selected = true;
				evt.currentTarget.gotoAndPlay("select");
			}
			
			
			trace("////// Question " + _id);
			var tab:Array = reponsesTab;
			for (var j = 0; j < tab.length; j++)
			{
				trace("  rep id : " + tab[j].id);
				trace("    label : " + tab[j].label);
				trace("    value : " + tab[j].value);
			}
		

		}
	}
}